﻿(function(){function l(g,a,e,c){this.atOccurrence=!1;this.cm=g;null==c&&"string"==typeof a&&(c=!1);e=e?g.clipPos(e):{line:0,ch:0};this.pos={from:e,to:e};if("string"!=typeof a)a.global||(a=RegExp(a.source,a.ignoreCase?"ig":"g")),this.matches=function(m,h){if(m){a.lastIndex=0;for(var f=g.getLine(h.line).slice(0,h.ch),d=a.exec(f),b=0;d;){b+=d.index+1;f=f.slice(b);a.lastIndex=0;var c=a.exec(f);if(c)d=c;else break}b--}else a.lastIndex=h.ch,f=g.getLine(h.line),b=(d=a.exec(f))&&d.index;if(d)return{from:{line:h.line,
ch:b},to:{line:h.line,ch:b+d[0].length},match:d}};else{c&&(a=a.toLowerCase());var i=c?function(a){return a.toLowerCase()}:function(a){return a},j=a.split("\n");this.matches=1==j.length?function(c,h){var f=i(g.getLine(h.line)),d=a.length,b;if(c?h.ch>=d&&-1!=(b=f.lastIndexOf(a,h.ch-d)):-1!=(b=f.indexOf(a,h.ch)))return{from:{line:h.line,ch:b},to:{line:h.line,ch:b+d}}}:function(a,c){var f=c.line,d=a?j.length-1:0,b=j[d],e=i(g.getLine(f)),k=a?e.indexOf(b)+b.length:e.lastIndexOf(b);if(!(a?k>=c.ch||k!=b.length:
k<=c.ch||k!=e.length-b.length))for(;!(a?!f:f==g.lineCount()-1);){e=i(g.getLine(f+=a?-1:1));b=j[a?--d:++d];if(0<d&&d<j.length-1)if(e!=b)break;else continue;d=a?e.lastIndexOf(b):e.indexOf(b)+b.length;if(a?d!=e.length-b.length:d!=b.length)break;b={line:c.line,ch:k};f={line:f,ch:d};return{from:a?f:b,to:a?b:f}}}}}l.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(g){function a(a){a={line:a,ch:0};e.pos={from:a,to:a};return e.atOccurrence=!1}
for(var e=this,c=this.cm.clipPos(g?this.pos.from:this.pos.to);;){if(this.pos=this.matches(g,c))return this.atOccurrence=!0,this.pos.match||!0;if(g){if(!c.line)return a(0);c={line:c.line-1,ch:this.cm.getLine(c.line-1).length}}else{var i=this.cm.lineCount();if(c.line==i-1)return a(i);c={line:c.line+1,ch:0}}}},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(g){this.atOccurrence&&(this.pos.to=this.cm.replaceRange(g,this.pos.from,
this.pos.to))}};CodeMirror.defineExtension("getSearchCursor",function(g,a,e){return new l(this,g,a,e)})})();